dir=$(cd "$(dirname "$0")"; pwd)
proj_dir=`dirname $dir`
hexo_dir="$proj_dir/export/mynote"
source_dir=$hexo_dir/source/_posts
rm -fr $source_dir

cd $dir
emacs --daemon -l config.emacs # start daemon
python3 build.py hexo $proj_dir $hexo_dir
emacsclient -e '(kill-emacs)' # stop daemon
cd $hexo_dir
hexo clean
hexo generate

cd $proj_dir
res_dirs=`find . -path ./export -prune -o -name ".*" -type d`
# find will ignore hidden dirs, so .git will not show
for dir in $res_dirs; do
	if test "$dir" = '.'; then
		continue
	fi
	if test "$dir" = '..'; then
		continue
	fi
	basename=$(basename $dir)
	target_dir=$hexo_dir/public/$(dirname $dir)/${basename:1}
	if test ! -d $target_dir; then
		mkdir -p $target_dir
	fi
	for ele in "$dir"/*; do
		if test -f $ele; then
			cp $ele $target_dir
		fi
	done
done

